<?php

class Technical_SubjectController extends Zend_Controller_Action {

	public function init() {

	}

	public function indexAction()
	{
		$session = new Zend_Session_Namespace('identity');
		$subjects = new Application_Model_Dao_Subject();
		$result = $subjects->getAll()->toArray();
		$this->view->subjects = $result;
		if( $result == null ) {
			$this->view->empty = "Brak przedmiotów w bazie.";
		} else {
			$this->view->empty = "";
		}

		// obiekty z sesji
		if( isset( $session->statusdel ) && $this->_getParam('statusdelete') == 'true' ) {
			$designation = $this->_getParam('designation');
			$this->view->delete = 'Rekord "'.$designation.'" został poprawnie usunięty.';
			unset( $session->statusdel );
		} else {
			$this->view->delete = "";
		}
		if( isset( $session->attention ) ) {
			$this->view->attention = $session->attention ;
			unset( $session->attention );
		} else {
			$this->view->attention = "";
		}

		if( isset( $session->operationsuccess ) ) {
			$this->view->operationStatus = $session->operationsuccess ;
			unset( $session->operationsuccess );
			if(isset( $session->operationdesignation ) ) {
				$this->view->operationDesignation = $session->operationdesignation;
				unset( $session->operationdesignation );
			} else {
				$this->view->operationDesignation = "";
			}
		} else {
			$this->view->deleteRecord = "";
		}

		$form = new Application_Form_Subject();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		// edycja oraz dodawanie w jquery, kod obsługujący requesta z danymi z formularza, zapis do bazy
		if( $this->getRequest()->isPost() ) {
			$formData = $this->getRequest()->getPost();
			if( $form->isValid($formData) ) {
				$subject = new Application_Model_Entities_Subject();
				$subject->setId( $form->getValue('id'));
				$subject->setDesignation( $form->getValue('designation'));
				$subject->setShort( $form->getValue('short'));
				$subjectDao = new Application_Model_Dao_Subject();
				if($form->getValue('typeForm') == 'editform' ) {
					$result = $subjectDao->updateSubject($subject);
					if($result > 0 ) {
						$session->operationsuccess = 'updatesuccess';
					} else {
						$session->operationsuccess = 'updatefail';
					}
				} else {
					$session->attention = 'Błędne dane dotyczące typu operacji';
				}
				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		} else {
			$session->attention = '';
		}
		//} else {
		//	$this->_helper->redirector( 'index', 'faculty', 'technical');
		//}
	}



	public function addAction() {
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_Subject();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		$form->getElement('typeForm')->setValue('addform');
		$form->submit->setLabel('Dodaj nowy przedmiot');
		$this->view->SubjectForm = $form;
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$subject = new Application_Model_Entities_Subject();
				$subject->setDesignation( $form->getValue('designation'));
				$subject->setShort( $form->getValue('short'));
				$subjectDao = new Application_Model_Dao_Subject();
				$result = $subjectDao->saveSubject($subject);
				if($result > 0 ) {
					$session->operationsuccess = 'addsuccess';
					$session->operationdesignation = $form->getValue('designation');
				} else {
					$session->operationsuccess = 'addfail';
				}
				$this->_helper->redirector('index');
			} else {
				$session->attention = 'Przesłane dane w formularzu nie są poprawne.';
				$this->_helper->redirector( 'index');
			}
		} else {
			$session->attention = '';
			//$this->_helper->redirector( 'index');
		}
	}

	public function editAction(){
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_Subject();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		$form->submit->setLabel('Zatwierdź zmiany');
		$this->view->SubjectEditForm = $form;
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$subject = new Application_Model_Entities_Subject();
				$subject->setId( $form->getValue('id'));
				$subject->setDesignation( $form->getValue('designation'));
				$subject->setShort( $form->getValue('short'));
				$subjectDao = new Application_Model_Dao_Subject();
				$subjectDao->updateSubject($subject);
				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		} else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$subjectDao = new Application_Model_Dao_Subject();
				$form->populate($subjectDao->getSubjectByID($id));
				$session->operationdesignation = $form->getValue('designation');
				$form->getElement('typeForm')->setValue('editform');
			} else {
				$session->attention = 'Błąd podczas pobierania danych lub podczas przypisywania ich do pól formularza.';
				$this->_helper->redirector( 'index');
			}
		}
	}
	public function deleteAction() {
		$session = new Zend_Session_Namespace('identity');
		$request = $this->getRequest();
		if( $request->isPost() ) {
			$del = $request->getPost('del');
			if( $del == 'Tak, usuń rekord' ) {
				$id = $request->getPost('id');
				$designation = $request->getPost('designation');
				$subject = new Application_Model_Entities_Subject();
				$subject->setId($id);
				$subjectDao = new Application_Model_Dao_Subject();
				$subjectDao->deleteSubject($subject);
				$session->statusdel = 'true';
				$this->_helper->redirector( 'index', 'subject', 'technical', array('statusdelete' => 'true', 'designation' => $designation));
			} else {
				$this->_helper->redirector( 'index');
			}
		} else //{
		//$session->attention = 'Dane prawdopodobnie nie zostały wysłane.';
		///$this->_helper->redirector( 'index');
		//}
		if( $this->_getParam('jsflag') == 'false' ) {
			$id = $this->_getParam('id', 0);
			if( $id > 0 ) {
				$subjectDao = new Application_Model_Dao_Subject();
				$result = $subjectDao->getRowSubject($id);
				$this->view->subjects = $result;
			} else {
				$session->attention = 'Błąd podczas pobierania danych lub podczas przypisywania ich do pól formularza.';
				$this->_helper->redirector( 'index');
			}
		} else if( $this->_getParam('jsflag') == 'true' ) {
			$id = $this->_getParam('id', 0);
			$subject = new Application_Model_Entities_Subject();
			$subject->setId($id);
			$subjectDao = new Application_Model_Dao_Subject();
			$result = $subjectDao->deleteSubject($subject);
			if($result > 0) {
				$session->operationsuccess = "deletesuccess" ;
				$session->operationdesignation = $this->_getParam('name', 0);
			} else {
				$session->operationdesignation = "";
				$session->operationsuccess = "deletefail" ;
			}

		} else {
			$this->_helper->redirector('index');
		}
	}


	/**
	 * Masowe działania to inaczej usuwanie wielu rekordów uprzednio zaznaczonych w formularzu
	 */
	public function massAction() {
		$request = $this->getRequest();
		if( $request->isPost() ) {
			/**
			 * Działania podjęte po wybraniu usuwania z select'a 'Masowe działania'
			 */
			if( $request->getPost('massAction') == 'deleteAll' ) {
				$id = $request->getPost('id');
				$subjectDao = new Application_Model_Dao_Subject();
				if ( sizeof($id) > 0 ) {
					$data = explode($request->getPost('id'));
					$designation = $request->getPost('designation');


					$this->view->data = array('id'=>$id, 'designation'=>$designation);
				} else {
					$session->attention = "Nie zaznaczono żadnego elementu do usunięcia.";
					$this->_helper->redirector('index');
				}
			} else {
				$session->attention = "Wybierz co chcesz zrobić z menu 'Masowe działania' ";
				$this->_helper->redirector('index');
			}

			/**
			 * Usuwanie wielu przedmiotow po zatwierdzeniu.
			 */
			if( $request->getPost('massActionSure') == 'deleteAll' ) {
				$id = $request->getPost('id');
				$designation = $request->getPost('designation');
				$ids;
				for($i=0; $i<sizeof($id); $i++)
				$ids[$i] = (int)$id[$i];
				$subjectDao = new Application_Model_Dao_Subject();
				$this->view->subjectsDelete = $subjectDao->deleteSubject($ids);
				$session = new Zend_Session_Namespace('identity');
				$session->statusdel = 'true';
				$this->_helper->redirector( 'index', 'subject', 'technical', array('statusdelete' => 'true', 'designation' => $designation));
			} else {
				$session->attention = "Wybierz co chcesz zrobić z menu 'Masowe działania' ";
				$this->_helper->redirector('index');
			}
		} else {
			$this->_helper->redirector('index');
		}
	}


}